package com.future.biz;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.dao.AreaDAO;
import com.future.model.bo.AreaBO;
import com.future.model.po.AreaModel;

public class AreaBIZ {
	private static Logger logger = LogManager.getLogger(AreaBIZ.class);

	public AreaBO generateLowerAreaBO(String code) {
		AreaBO area = new AreaBO();
		AreaDAO areaDao = new AreaDAO();
		List<AreaModel> areaList = areaDao.fuzzyByCode(codeHandler(code));
		area.setLastAreaCode(code);
		area.setAreaList(areaList);
		return area;
	}
	public AreaBO generateLowerAreaBO(String code,String flag) {
		AreaBO area = new AreaBO();
		AreaDAO areaDao = new AreaDAO();
		List<AreaModel> areaList = areaDao.fuzzyByCode(codeHandler(code),code);
		area.setLastAreaCode(code);
		area.setAreaList(areaList);
		return area;
	}

	public AreaBO generateHigherAreaBO(String code) {
		AreaBO area = new AreaBO();
		return area;
	}

	private String codeHandler(String code) {
		int cityFlag = code.lastIndexOf("0000000000");
		int townFlag = code.lastIndexOf("00000000");
		int otherFlag = code.lastIndexOf("000000");
		int other = code.lastIndexOf("000");

		if (cityFlag > 0) {
			return code.substring(0, 2) + "[0-9][0-9][0]{8}";
		} else if (townFlag > 0) {
			return code.substring(0, 4) + "[0-9][0-9][0]{6}";
		} else if (otherFlag > 0) {
			return code.substring(0, 6) + "[0-9]{3}[0]{3}";
		} else if (other > 0) {
			return code.substring(0, 9) + "[0-9]{3}";
		} else{
			return code;
		}
	}
}
